<?
/*
%%%copyright%%%
 * phpMyTicket - ticket reservation system
 * Copyright (C) 2004-2005 Anna Putrino, Stanislav Chachkov. All rights reserved.
 *
 * This file is part of phpMyTicket.
 *
 * This file may be distributed and/or modified under the terms of the
 * "GNU General Public License" version 2 as published by the Free
 * Software Foundation and appearing in the file LICENSE included in
 * the packaging of this file.
 *
 * Licencees holding a valid "phpmyticket professional licence" version 1
 * may use this file in accordance with the "phpmyticket professional licence"
 * version 1 Agreement provided with the Software.
 *
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING
 * THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE.
 *
 * The "phpmyticket professional licence" version 1 is available at
 * http://www.phpmyticket.com/ and in the file
 * PROFESSIONAL_LICENCE included in the packaging of this file.
 * For pricing of this licence please contact us via e-mail to 
 * info@phpmyticket.com.
 * Further contact information is available at http://www.phpmyticket.com/
 *
 * The "GNU General Public License" (GPL) is available at
 * http://www.gnu.org/copyleft/gpl.html.
 *
 * Contact info@phpmyticket.com if any conditions of this licencing isn't 
 * clear to you.
 
 */
?>
<?
require_once("admin/AdminView.php");
require_once("admin/PlaceMapZoneView.php");
require_once("admin/PlaceMapPartView.php");
require_once("admin/PlaceMapCategoryView.php");

require_once("classes/ShopDB.php");
require_once("classes/PlaceMap.php");

class PlaceMapView2 extends AdminView{


function pm_view ($pm_id, $pm=null){
  global $_SHOP;

  if(!isset($pm)){
    $pm=PlaceMap::load($pm_id);
  }
  
  if($pm->pm_event_id){
    require_once('classes/Event.php');
    $event=Event::load($pm->pm_event_id,FALSE);
  }
  
//title
  echo "<table class='admin_form' width='500' cellspacing='1' cellpadding='4'>
        <tr><td class='admin_list_title' colspan='2'>
	{$pm->pm_name} ({$pm->ort_name})
        	";	
  if($event){
    echo " <a class='link' href='{$_SERVER['PHP_SELF']}?action=edit_pm&pm_id={$pm->pm_id}'><img src='images/edit.gif' border='0' alt='".edit."' title='".edit."'></a>
    <br>{$event->event_name} {$event->event_date} {$event->event_time}";
    $live = $event->event_status!='unpub';
  }
  echo "</td></tr></table><br>";

  $pmz_view=new PlaceMapZoneView;
  $pmz_view->pmz_list($pm_id,$live);
  echo "<br>";
  $pmp_view=new PlaceMapCategoryView;
  $pmp_view->category_list($pm_id,$live);
  echo "<br>";
  $pmp_view=new PlaceMapPartView;
  $pmp_view->pmp_list($pm_id,$live);

  if($pm->pm_organizer_id==$_SHOP->organizer_id){
    echo "<br><center><a class=link href='{$_SERVER['PHP_SELF']}?action=split_pm&pm_id=$pm_id'>".split_pm."</a></center>";
  }
  
  if($pm->pm_image){
   echo "<br><center><img src='".$this->user_url($pm->pm_image)."'></center>";
  }
  
//ending

  echo "<center><br>";
   
  if($pm->pm_event_id){  
    if($event->event_rep=='sub'){   
        echo "<a href='view_event_sub.php?event_main_id={$event->event_main_id}' class='link'>".admin_list."</a>";
    }else{
        echo "<a href='view_event.php' class='link'>".admin_list."</a>";
    }
  }else{
        echo "<a href='view_ort.php?action=view&ort_id={$pm->pm_ort_id}' class=link>".admin_list."</a>";  
  }	
  echo "</center>";

}


function pm_form (&$data, &$err, $title){
  echo "<form method='POST' action='{$_SERVER['PHP_SELF']}' enctype='multipart/form-data'>";
  
  $this->form_head($title);
      
  $this->print_field_o('pm_id',$data );
  $this->print_field('ort_name',$data);

  $this->print_input('pm_name',$data,$err,30,100);
  $this->print_file('pm_image',$data,$err,'img');  	
  
//  $this->print_input('pm_width',$data,$err,4,3);
//  $this->print_input('pm_height',$data,$err,4,3);

  $this->form_foot();

  if($data['pm_id']){
    echo "<input type=hidden name=action value=update_pm>
          <input type=hidden name=pm_id value='{$data['pm_id']}'>";
  }else{
    echo "<input type=hidden name=action value=insert_pm>";
  }
    
  echo"<input type=hidden name=pm_event_id value='{$data['pm_event_id']}'>
    <input type=hidden name=pm_ort_id value='{$data['pm_ort_id']}'>
    </form><br><center><a class='link' href='{$_SERVER['PHP_SELF']}?action=view&ort_id={$data['pm_ort_id']}'>
    ".admin_list."</a></center>";
}

function pm_check (&$data, &$err){
  if(empty($data['pm_name'])){$err['pm_name']=mandatory;} 

  /*if(!$data['pm_width'] or $data['pm_width']<=0){
    $err['pm_width']=invalid;
  }

  if(!$data['pm_height'] or $data['pm_height']<=0){
    $err['pm_height']=invalid;
  }
*/
  return empty($err);
}

function pm_list ($ort_id){
  global $_SHOP;

  $query="select ort_organizer_id from Ort where ort_id=$ort_id";
  if(!$ort=ShopDB::query_one_row($query)){return;}
  if($ort['ort_organizer_id']==$_SHOP->organizer_id){
    $mine=TRUE;
  }

  $query="select * from PlaceMap2 where pm_ort_id=$ort_id and pm_event_id IS NULL";
  if(!$res=ShopDB::query($query)){return;}
  
  $this->list_head(place_maps,3);
  $alt=0;
  
  while($pm=mysql_fetch_array($res)){
     echo "<tr class='admin_list_row_$alt'>";
     
     echo "<td class='admin_list_item'>{$pm['pm_id']}</td>\n";
     echo "<td class='admin_list_item' width='70%'>{$pm['pm_name']}</td>\n";
     
     echo "<td class='admin_list_item' align=right><a class='link' href='view_pm2.php?action=view_pm&pm_id={$pm['pm_id']}'><img src='images/view.png' border='0' alt='".view."' title='".view."'></a>\n";

     if($pm['pm_organizer_id']==$_SHOP->organizer_id){
       echo "<a class='link' href='{$_SERVER['PHP_SELF']}?action=edit_pm&pm_id={$pm['pm_id']}'><img src='images/edit.gif' border='0' alt='".edit."' title='".edit."'></a>\n";
       echo "<a class='link' href='{$_SERVER['PHP_SELF']}?action=copy_pm&pm_id={$pm['pm_id']}&pm_ort_id={$pm['pm_ort_id']}'><img src='images/copy.png' border='0' alt='".copy."' title='".copy."'></a>\n";
       echo "<a class='link' href='javascript:if(confirm(\"".delete_item."\")){location.href=\"{$_SERVER['PHP_SELF']}?action=remove_pm&pm_id={$pm['pm_id']}&pm_ort_id={$pm['pm_ort_id']}\";}'><img src='images/trash.png' border='0' alt='".remove."' title='".remove."'></a>\n";
     }
     echo "</td></tr>";
     $alt=($alt+1)%2;
  }
  
  if($mine){
    echo "<tr><td colspan=6 align=center><a class='link' href='view_pm2.php?action=add&pm_ort_id=$ort_id'>".add."</a></td></tr>";
  }
  
  echo "</table>";

}

  function split_form ($pm_id) {
    global $_SHOP;
  
    if(!$pm=PlaceMap::load($pm_id) and $_SHOP->organizer_id!=$pm->pm_organizer_id){return;}
    if(!$pm_parts=PlaceMapPart::loadAll($pm_id)){return;}
    
    echo "<form action='{$_SERVER['PHP_SELF']}' method=POST>";
    $this->form_head(pm_split);

    echo "<tr><td class='admin_name'  width='40%' >".split_pm."</td>
          <td class='admin_value'>
          <select name='pm_parts[]' multiple>\n";
  
    foreach($pm_parts as $pmp){
      echo "<option value='{$pmp->pmp_id}'>{$pmp->pmp_name}</option>\n";
    }
    echo "</select></td></tr>\n";

    $this->print_checkbox('split_zones',$data,$err);
    $this->form_foot();

    echo "
    <input type='hidden' name='action' value='split_pm'>
    <input type='hidden' name='pm_id' value='$pm_id'>
    </form>
    <br>
    <center><a class=link href='{$_SERVER['PHP_SELF']}?action=view_pm&pm_id=$pm_id'>".place_map."</a></center>
    ";
  }


  function draw (){
    global $_SHOP;
  
     
    if(preg_match('/_pmp$/',$_POST['action']) or preg_match('/_pmp$/',$_GET['action'])){
      $pmp_view=new PlaceMapPartView;  
      if($pmp_view->draw()){
        $this->pm_view($_POST['pm_id']+$_GET['pm_id']);
      }

    }elseif(preg_match('/_pmz$/',$_POST['action']) or preg_match('/_pmz$/',$_GET['action'])){
      $pmz_view=new PlaceMapZoneView;  
      if($pmz_view->draw()){
        $this->pm_view($_POST['pm_id']+$_GET['pm_id']);
      }

    }elseif(preg_match('/_category$/',$_POST['action']) or preg_match('/_category$/',$_GET['action'])){
      $view=new PlaceMapCategoryView;  
      if($view->draw()){
        $this->pm_view($_POST['pm_id']+$_GET['pm_id']);
      }



    }elseif($_GET['action']=='view_pm' and $_GET['pm_id']>0){
      $this->pm_view( $_GET['pm_id'] );      


    }else if($_GET['action']=='add'){
      $this->pm_form($_GET,$err,'');   

    }else if($_POST['action']=='insert_pm'){
      if(!$this->pm_check($_POST,$err)){
        $this->pm_form($_GET,$err,add_pm);
      }else{
        $pm = new PlaceMap($_POST['pm_ort_id'],$_POST['pm_name']);
	$pm->pm_organizer_id=$_SHOP->organizer_id;
        if($pm_id=$pm->save()){
	
          if(!$this->photo_post($_POST,$pm_id)){
            echo "<div class=error>".img_loading_problem."<div>";
          }
	
	  $this->pm_view($pm_id);
	}else{
	  echo '<div class=error>'.cannot_insert_pm.'</div>';
	  $this->pm_form($_GET,$err,add_pm);
	}
      }

    }else if($_GET['action']=='copy_pm' and $_GET['pm_id']>0){
          if($pm=PlaceMap::load($_GET['pm_id'])){
            $pm->copy();
    	    return TRUE;
	  
	  }
            
    }else if($_GET['action']=='edit_pm' and $_GET['pm_id']>0){
	  $query="select * from PlaceMap2,Ort where pm_id={$_GET['pm_id']} and pm_ort_id=ort_id";
	  if($row=ShopDB::query_one_row($query)){
  	    $this->pm_form($row,$err,edit_pm);
     	  }      
    }else if($_POST['action']=='update_pm' and $_POST['pm_id']>0){
          if(!$this->pm_check($_POST,$err)){
	    $this->pm_form($_POST['pm_id'],$err,edit_pm);
	  }else{
	    $pm=PlaceMap::load($_POST['pm_id']);
	    $pm->pm_name=$_POST['pm_name'];
	    $pm->save();

            if(!$this->photo_post($_POST,$pm->pm_id)){
              echo "<div class=error>".img_loading_problem."<div>";
            }

	    if($pm->pm_event_id){
	      $this->pm_view($pm->pm_id);
	    }else{
	      return TRUE;
	    }  
	  }  
    }else if($_GET['action']=='remove_pm' and $_GET['pm_id']>0){
      $pm=PlaceMap::load($_GET['pm_id']);
      //if($pm->pm_organizer_id==$_SHOP->organizer_id){
        $pm->delete();
      //}
      return TRUE;

    }else if($_GET['action']=='split_pm' and $_GET['pm_id']>0){
      $this->split_form($_GET['pm_id']);

    }else if($_POST['action']=='split_pm' and $_POST['pm_id']>0){
      
      if(!$pm=PlaceMap::load($_POST['pm_id']) or $pm->pm_organizer_id!=$_SHOP->organizer_id){return;}
      //print_r($_POST);
      $pm->split($_POST['pm_parts'],$_POST['split_zones']);
      $this->pm_view($_POST['pm_id']);

    }else if($_GET['pm_ort_id']){
      $this->pm_list($_GET['pm_ort_id']);

    }
  }

function photo_post ($data, $pm_id){
  return $this->file_post($data,$pm_id, 'PlaceMap2', 'pm','_image');
}

}

?>
